import router from '../../router/index'
import store from '../../store/index'

const whiteList = ['/login'] // 不重定向白名单

router.beforeEach((to, from, next) => {

  if (store.state.token) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
                store.dispatch('Menus').then((res1)=>{
                    console.log(res1,'res1')
                    router.addRoute(res1);
                    router.options.routes = res1;
                    console.log(router,"router")
                    next();
                }).then(()=>{
                  next({...to,replace:true});
                }).catch(err=>{
                  
                })
            // next();
    }
 
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
    }
  }
})

